/**
 * 
 */

function Redrawer()
{
	// Globals
	
	var globalThis = this;
	
	
	this.index = 0; // Shape number in data
	
	this.point = 0; // Current redrawing point from shape index
	
	this.timer = null;
	
	this.running = false;
	
	this.speed = 1;
	
	
	this.fullSpeed = true;
	
	this.isFullSpeed = function(){
		return this.fullSpeed;
	}
	
	this.setFullSpeed = function(state){
	
		this.fullSpeed = state;
	}
	
	this.setSpeed = function(speed){
		console.log("setting speed -> " + speed);
		if (speed==0) {speed = 1}
		this.speed = speed;
	}
	
	this.start = function(data, canvasManager)
	{
		current_id = null;
		
		canvasManager.clearCanvas(0,
						 0,
						 canvasManager.frontCanvas.getCanvas().width,
						 canvasManager.frontCanvas.getCanvas().height,
						 canvasManager.frontCanvas);
						 
		canvasManager.clearCanvas(0,
						 0,
						 canvasManager.backCanvas.getCanvas().width,
						 canvasManager.backCanvas.getCanvas().height,
						 canvasManager.backCanvas);
		
		this.running = true;
		
		this.index = 0; // Shape number in data
		
		this.point = 0; // Current redrawing point from shape index
		
		var time = 0;
		
		if (this.isFullSpeed()){
			time = data[this.index].time[this.point+1] - 
										data[this.index].time[this.point];
			time *= this.speed;
		}
		else{
			time = 0;
		}
		
		time = 0;
		
		this.timer = setTimeout(function() {globalThis.redrawShape(data, canvasManager);}, time);
	};
	
	/**
	 * Will draw the current shape instantly and then stop ... no
	 */
	this.stop = function()
	{
		this.running = false;
		
		clearTimeout(this.timer);
		
		this.point = 0;
		
		
	};
	
	this.pause = function()
	{
		
		clearTimeout(this.timer);
	};
	
	this.unpause = function(data, canvasManager)
	{
		var time = 0;
		
		if (this.isFullSpeed()){
			time = data[this.index].time[this.point+1] - 
										data[this.index].time[this.point];
			time *= this.speed;
		}
		else{
			time = 0;
		}
		
		clearTimeout(this.timer);
		
		this.timer = setTimeout(function() {globalThis.redrawShape(data, canvasManager);}, time);
	};
	
	this.redrawShape = function(data, canvasManager)
	{
		
		canvasManager.getBackCanvas().getContext().strokeStyle = data[this.index].color.toString();
		canvasManager.getBackCanvas().getContext().lineWidth = data[this.index].thickness;
		canvasManager.getBackCanvas().getContext().lineJoin = "round";

		
	/*if (this.transform){
				canvas[STATIC_CONTEXT].save();
			
				/* Translating to the center of the canvas 
				canvas[STATIC_CONTEXT].translate(shape_manager.data[this.index].real_x, shape_manager.data[this.index].real_y );
				canvas[STATIC_CONTEXT].scale(shape_manager.data[this.index].scale_x, shape_manager.data[this.index].scale_y);		
				canvas[STATIC_CONTEXT].translate(-shape_manager.data[this.index].real_x, -shape_manager.data[this.index].real_y );
			//}*/
		
		canvasManager.drawPoint(data[this.index], this.point, canvasManager.getBackCanvas());
		
		this.point++;
		if (this.point >= data[this.index].x.length - 1)
		{
			this.index++;
			this.point = 0;
			
			if (this.index >= data.length)
			{
				//clearTimeout(this.timer);
				this.stop();
			}
			else
			{
				var time = 0;
				clearTimeout(this.timer);
				if (this.isFullSpeed()){
					time = data[this.index].time[this.point+1] - 
												data[this.index].time[this.point];
												
					time *= this.speed;
				}
				else{
					time = 0;
				}
				
				time = 0;
										
				this.timer = setTimeout(function() {globalThis.redrawShape(data, canvasManager);}, time);

			}
		}
		else
		{
			var time = 0;
			clearTimeout(this.timer);
			if (this.isFullSpeed()){
				time = data[this.index].time[this.point+1] - 
					   data[this.index].time[this.point];
				time *= this.speed;
			}
			else{
				time = 0;
			}
			this.timer = setTimeout(function() {globalThis.redrawShape(data, canvasManager);}, time);
		}
		
		//if (this.transform){
				//canvas[STATIC_CONTEXT].restore();
			//	}
		
	};
}